package com.demo.example;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 批量更新的方法
 * @author gy
 * @date 2022/7/21
 */
public class BatchUpdateExa {
    /**
     * 特点一：sql 采取分页查询
     * 特点二：将update的数据进行批量存储、批量更新
     * 特点三：do while 循环实现查询多余数据处理（当查询结果为0时退出）
     * 特点四：使用 AtomicInteger 实现更新数量的统计
     * @return
     */
//    public static boolean synTotalTeacherHour() {
//    int page = 0;
//    int pageSize = 100;
//    int check = 0;
//    AtomicInteger atomicInteger = new AtomicInteger(0);
//    do {
//        List<String> traineeSql = Lists.newArrayList();
//        try {
//            StringBuffer buffer = new StringBuffer();
//            buffer.append("  SELECT                                                                            ");
//            buffer.append("    pt.ID ptId,                                                                          ");
//            buffer.append("   SUM(IFNULL(ptp.studyHours,'0')) studyHours                                              ");
//            buffer.append("  FROM                                                                              ");
//            buffer.append("    pe_trainee pt                                                                 ");
//            buffer.append("    LEFT JOIN pe_teaching_plan ptp ON ptp.fk_teacher_id = pt.ID                   ");
//            buffer.append("    LEFT JOIN pe_training_class ptc ON ptc.ID = ptp.FK_TRAIN_CLASS                ");
//            buffer.append("    JOIN enum_const ec ON ec.ID = ptc.flag_is_open_class                          ");
//            buffer.append("  WHERE                                                                             ");
//            buffer.append("   ec.`CODE` = '13'                                                                 ");
//            buffer.append("  GROUP BY                                                                          ");
//            buffer.append("    pt.ID                                                                         ");
//            buffer.append("  limit              " +page*pageSize+","+pageSize  );
//            List<Map<String,Object>> traineeList = generalService.getBySQLToMap(buffer.toString());
//            check = traineeList.size();
//            if (CollectionUtils.isNotEmpty(traineeList)) {
//                for (int i = 0; i < traineeList.size(); i++) {
//                    Map<String, Object> map = traineeList.get(i);
//                    traineeSql.add("UPDATE pe_trainee pt SET pt.total_class_hours = '"+map.get("studyHours")+"' WHERE pt.ID = '"+map.get("ptId")+"'; ");
//                    logger.info(">>>>>>>>>>"+map.get("ptId")+">>>>>"+map.get("studyHours"));
//                }
//                int num = generalService.batchExecuteSql(traineeSql);
//                if(atomicInteger.compareAndSet(num,atomicInteger.get()+num)){
//                    logger.info("批量处理学时数量为："+atomicInteger.get());
//                }
//            }
//        } catch (EntityException e) {
//            e.printStackTrace();
//        }
//        page++;
//    }while (check>0);
//    return true;
}

    

